#include <stdio.h>
#include <stdlib.h>
#include "linktest.h"

link_node *creat()
{
	link_node *H=(link_node *)malloc(sizeof(link_node));
	H->data=0;
	H->next=NULL;

	return H;
}

int Hinsert(link_node *H)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	printf("plz input num\n");
	scanf("%d",&(p->data));
	p->next=H->next;
	H->next=p;
	H->data++;

	return 0;
}

int Tinsert(link_node *H)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	link_node *q=H;
	while(q->next!=NULL)
	{
		q=q->next;
	}
	printf("plz input num\n");
	scanf("%d",&(p->data));
	p->next=NULL;
	q->next=p;
	H->data++;

	return 0;
}

int posinsert(link_node *H)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	link_node *q=H;
	int pos=0;
	printf("plz input num\n");
	scanf("%d",&(p->data));
	printf("plz input pos\n");
	scanf("%d",&pos);
	for(int i =0;i<pos;i++)
	{
		q=q->next;
	}
	p->next=q->next;
	q->next=p;
	H->data++;

	return 0;
}

int Hdelete(link_node *H)
{
	link_node *p=H->next;
	link_node *q=H;
	q->next=p->next;

	free(p);
	H->data--;

	return 0;
}

int Tdelete(link_node *H)
{
	if(H->next==NULL)
	{
		printf("empty\n");
		return 0;
	}
	link_node *p=H;
	link_node *q=H;
	while(p->next->next!=NULL)
	{
		p=p->next;
	}
	q=p->next;
	p->next=q->next;
	free(q);

	H->data--;

	return 0;
}

int posdelete(link_node *H)
{
	int pos = 0;
	link_node *p=H;
	link_node *q=H;
	printf("plz input pos\n");
	scanf("%d",&pos);
	for(int i =0;i<pos-1;i++)
	{
		q=q->next;
	}
	p=q->next;
	q->next=p->next;
	free(p);
	H->data--;

	return 0;
}

int view(link_node *H)
{
	link_node *p=H;
	while(p->next!=NULL)
	{
		p=p->next;
		printf("%d ",p->data);
	}
	putchar(10);

	return 0;
}
